/****************************************************************************/
/*                                                                          */
/*   FILE:    trans.h	                                                    */
/*   DATE:    13/06/97                                                      */
/*   PURPOSE: Header File for DVB-CI Transport layer                        */
/*   TAB STOPS : 4															*/
/*   HISTORY:				                                    			*/
/*   Version    Date	  	    Written by	     Notes	    	    		*/
/*	 1.0	    13/06/1997      SCM              Initial implementation 	*/
/*                                                                    	    */
/*--------------------------------------------------------------------------*/
/*   GENERAL COMMENTS                                                 	    */
/*									    									*/
/****************************************************************************/
#ifndef __TRANS_H
#define __TRANS_H

#include "xmsg.h"

#ifdef __cplusplus
extern "C" {
#endif

/*--------------------------------------------------------------------------*/
/* Symbol Definitions                                                       */
/*--------------------------------------------------------------------------*/
/* states of transport state diagram */
#define TRN_NOT_USED	1
#define TRN_IDLE 		2
#define TRN_CREATION 	3
#define TRN_ACTIVE		4
#define TRN_POLLING		5
#define TRN_POLLMORE	6
#define TRN_RECEPTION	7
#define TRN_DELETION	8

#define GETTRANSTATNAME(transtat) ((transtat)==TRN_NOT_USED?"TRN_NOT_USED":	((transtat)==TRN_IDLE?"TRN_IDLE":((transtat)==TRN_CREATION?"TRN_CREATION":((transtat)==TRN_ACTIVE?"TRN_ACTIVE":	((transtat)==TRN_POLLING?"TRN_POLLING":	((transtat)==TRN_POLLMORE?"TRN_POLLMORE":((transtat)==TRN_RECEPTION?"TRN_RECEPTION":((transtat)==TRN_DELETION?"TRN_DELETION":"UNKN_TANSTAT"))))))))

#define FIRST_VALID_TCID		1			/* first tcid 								*/

/*--------------------------------------------------------------------------*/
/* Structures Definitions                                                   */
/*--------------------------------------------------------------------------*/
typedef struct
{
	unsigned short		m_slotId;			/* reference to physical link			*/
	unsigned char		m_state;			/* state of transport connection		*/
	unsigned char		m_tcid;			/* transport connection number			*/
	unsigned short		m_timerId;			/* timeout timer identifier				*/
	unsigned short		m_pollTimerId;		/* polling timer identifier				*/
	unsigned char*		m_pbSpdu;			/* current SPDU split in several TPDU*/
	unsigned char*		m_pbSend;			/* pointer to the sending complete SPDU */
	unsigned long		m_len;				/* offset in current SPDU split in several TPDU				  */
	unsigned char*		m_pbRcvSpdu;			/* current SPDU concatenated with several received TPDU			  */
	unsigned long		m_recvSize;			/* offset in current SPDU concatenated with several received TPDU */
	unsigned char		m_tag;				/* Last TPDU received (for request_t_c)	*/
}trpt_t;		/* informations for one transport connection				*/
	
/*--------------------------------------------------------------------------*/
/* Function Prototypes                                                      */
/*--------------------------------------------------------------------------*/

int  transport(void* param);			/* Entry point of TRANS Task				*/

int  TRPT_Init(void);			/* Initialization Module					*/
void TRPT_State(xmsg_t* pXmsg);	        /* Entry point of the Transport State Diagram Module */
void TRPT_End(void);			/* Ending module 							*/

int  TRPT_Connect(xmsg_t* pXmsg);		/* TP_CONNECT received from LINK Task		*/
int  TRPT_Disconnect(xmsg_t* pXmsg);		/* TP_DISCONNECT received from LINK Task	*/
int  TRPT_Tpdu(xmsg_t* pXmsg);			/* TPDU received from LINK Task				*/
int  TRPT_Spdu(xmsg_t* pXmsg);			/* SPDU received from SESS Task				*/
int  TRPT_Delete(xmsg_t* pXmsg);			/* SS_DELETE_TC received from SESS Task		*/
int  TRPT_Timeout(xmsg_t* pXmsg);		/* TIMEOUT received							*/
int  TRPT_TmPoll(xmsg_t* pXmsg);			/* TM_POLL received							*/
              
int  TRPT_AllocTcidIndex(void);		/* get a new entry in g_trn_tab_tcid			*/
int  TRPT_FreeTcidIndex(int trnTcidIndex); /* delete a entry in g_trn_tab_tcid				*/
int  TRPT_GetTcidIndex(unsigned  char tcid);	/* get the trnTcidIndex in g_trn_tab_tcid		*/
int  TRPT_GetTcidIndexByTM(unsigned  short timerId);/* get the trnTcidIndex in g_trn_tab_tcid according to a TIMEOUT timer	  */
int  TRPT_GetTcidIndexByTMPoll(unsigned  short timerId); /* get the trnTcidIndex in g_trn_tab_tcid according to a TM_POLL timer */
void TRPT_Kill_Timer(int trnTcidIndex);/* kill a pending TIMEOUT timer				*/
void TRPT_Kill_PollTimer(int trnTcidIndex);/* kill a pending TIMEPOLL timer		*/

/*--------------------------------------------------------------------------*/
/* State Diagram                                                      */
/*--------------------------------------------------------------------------*/
int TRPT_Action(int trnTcidIndex, xmsg_t* pXmsg);		/* Entry point  	*/
int TRPT_Idle(int trnTcidIndex, xmsg_t* pXmsg);							/* idle state		*/
int TRPT_Creation(int trnTcidIndex, xmsg_t* pXmsg);	/* creatation state	*/
int TRPT_Active(int trnTcidIndex, xmsg_t* pXmsg);		/* active state		*/
int TRPT_Polling(int trnTcidIndex, xmsg_t* pXmsg);		/* polling state	*/
int TRPT_Pollmore(int trnTcidIndex, xmsg_t* pXmsg);	/* pollmore state	*/
int TRPT_Reception(int trnTcidIndex, xmsg_t* pXmsg);	/* reception state	*/
int TRPT_Deletion(int trnTcidIndex, xmsg_t* pXmsg);	/* deletion state	*/

int TRPT_Disc(int trnTcidIndex);							/* send a SS_TC_DELETED message					*/
int TRPT_Release(int trnTcidIndex);	/* free the transport connection				*/

/*--------------------------------------------------------------------------*/
/* TPDU Code/Decode                                                      */
/*--------------------------------------------------------------------------*/               
int TRPT_Code(unsigned char tag, unsigned char tcid, xpdu_t *pXpdu, unsigned char *pbTpdu);
unsigned long TRPT_Decode(unsigned char *pbTpdu, unsigned long len, unsigned char tcid, 
				unsigned char *tag, xpdu_t *pXpdu);
unsigned long TRPT_XmsgDecode(xmsg_t* pXmsg, unsigned char *pbTpduTag, 
					unsigned char* pbTcid, xpdu_t *pXpdu);
// code length field
void TRPT_SetLength(unsigned char *ptr, unsigned long length, unsigned short *lenlen);
// decode length field 
int  TRPT_GetLength(unsigned char *ptr, unsigned long *length, unsigned short *lenlen);



/*--------------------------------------------------------------------------*/
/* fifo                                                       */
/*--------------------------------------------------------------------------*/

#define TRN_MAXFIFOELT 200		/* 10 entries for a fifo 						*/ //ytdiao
typedef struct
{
	int					m_head;					/* fifo's head 								*/
	int					m_tail;					/* fifo's tail 								*/ 
	int					m_count;					/* number of elements in fifo				*/
	tran_spdu_t			m_arItems[TRN_MAXFIFOELT];   /* element's fifo 							*/
	unsigned char		m_tag[TRN_MAXFIFOELT];	/* saved tag (SPDU or SS_DELETE_TC)		*/
}trn_fifo_t;
	
void		  TRPT_ResetFifo(int trnTcidIndex);									/* Clear Fifo					*/
int			  TRPT_PutFifo(int trnTcidIndex, unsigned char tag, tran_spdu_t *ptr);	/* Put a SPDU into the FIFO		*/
unsigned char TRPT_GetFifo(int trnTcidIndex, tran_spdu_t *ptr);						/* Get a SPDU into the FIFO		*/


#ifdef __cplusplus
}
#endif


#endif	/* !__TRANS_H */
